//
//  WQVisitorView.swift
//  WeiBo
//
//  Created by 张德华 on 7/23/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit
import SnapKit
class WQVisitorView: UIView {
    
    // MARK: - ************属性定义
    ///------ 回调闭包
    var visitorClosure:(()->())?
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(red: 0.93, green: 0.93, blue: 0.93, alpha: 1)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - ************设置访客视图信息(外界调用)
    func setupVisitorViewInfo(imageName: String?, title: String?) {
        
        if let img = imageName, let titl = title {
            feedImageView.hidden = true
            houseImageView.image = UIImage(named: img)
            messageLabel.text = titl
        } else {
            setupAnim()
        }
    }
    
    
    
    // MARK: - ************圆圈动画
    func setupAnim() {
        let anim: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        feedImageView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    // MARK: - ************监听事件
    ///------ 登录按钮监听
    @objc func loginButtonClicked() {
        
        print("visitorView->登录按钮")
        visitorClosure?()
    }
    
    ///------ 注册按钮监听
    @objc func registerButtonClicked() {
        
        print("visitorView->注册按钮")
    }
    
    
    
    
    
    
    
    // MARK: - ************设置界面
    func setupUI() {
        
        ///------ 添加控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        
        ///------ 设置约束
        feedImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        houseImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 230, height: 36))
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
        }
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
    }

    // MARK: - ************懒加载控件
    ///------ 圆圈
    private lazy var feedImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage.init(named: "visitordiscover_feed_image_smallicon")
        return imageView
    }()
    ///------ 遮罩
    private lazy var maskImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage.init(named: "visitordiscover_feed_mask_smallicon")
        return imageView
    }()
    ///------ 房子
    private lazy var houseImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage.init(named: "visitordiscover_feed_image_house")
        return imageView
    }()
    ///------ 文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        return label
    }()
    ///------ 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: #selector(WQVisitorView.loginButtonClicked), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    ///------ 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: #selector(WQVisitorView.registerButtonClicked), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
}
